package view.server;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import controller.Controller;

import model.Job;

public class EditJobListPanelRenderer extends JLabel implements
		ListCellRenderer {

	public EditJobListPanelRenderer(Controller controller) {
	}

	private static final long serialVersionUID = 1L;

	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {

		Job job = (Job) value;
		setText("<html> " + removeSpecialCharactersFromJobName(job.getJobName()) + "</html>");

		if (!isSelected) {
			setForeground(Color.BLACK);
			setBackground(Color.WHITE);
		} else {
			setForeground(Color.WHITE);
			setBackground(new Color(57, 105, 138));
		}

		setOpaque(true);
		return this;
	}

	private String removeSpecialCharactersFromJobName(String jobName) {
		String formattedJobName = "";

		for (int i = 0; i < jobName.length(); i++) {
			if (((jobName.charAt(i) != '#') && (jobName.charAt(i) != '+'))
					|| ((jobName.charAt(i) == '#') && (jobName.charAt(i - 1) == '&'))) {
				formattedJobName += jobName.charAt(i);
			}
		}
		return formattedJobName;
	}
}
